package com.atguigu.educms.controller;


import com.atguigu.commonutils.R;
import com.atguigu.educms.entity.BannerQuery;
import com.atguigu.educms.entity.CrmBanner;
import com.atguigu.educms.service.CrmBannerService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前台bannber显示
 * </p>
 *
 * @author 若chen
 * @since 2022-04-23
 */
@RestController
@RequestMapping("/educms/bannerfront")
@CrossOrigin
public class BannerFrontController {

    @Autowired
    private CrmBannerService bannerService;



    //查询所有banner
    @Cacheable(value = "banner",key = "'selectIndexList'")
    @GetMapping("getAllBanner")
    public R getAllBanner() {
        List<CrmBanner> list = bannerService.selectAllBanner();
        return R.ok().data("list",list);
    }


    @ApiOperation("查询全部轮播图")
    @GetMapping("/findAllBanner")
    public R findAllBanner() {
        List<CrmBanner> allBanner = bannerService.findAllBanner();
        return R.ok().data("bannerList", allBanner);
    }

    @ApiOperation("分页查询轮播图")
    @GetMapping("/pageBanner/{current}/{limit}")
    public R pageBanner(@ApiParam(value = "起始页", required = true)
                        @PathVariable("current") Long current,
                        @ApiParam(value = "末页", required = true)
                        @PathVariable("limit") Long limit) {
        Page<CrmBanner> bannerPage = new Page<>(current, limit);
        bannerService.page(bannerPage, null);
        return R.ok().data("bannerList", bannerPage.getRecords()).data("total", bannerPage.getTotal());
    }

    @ApiOperation("添加轮播")
    @PostMapping("/addBanner")
    public R addBanner(@RequestBody BannerQuery bannerQuery) {
        bannerService.saveBanner(bannerQuery);
        return R.ok();
    }

    @ApiOperation("删除轮播")
    @DeleteMapping("/deleteBannerBy/{id}")
    public R deleteBannerById(@PathVariable("id") String id) {
        bannerService.removeById(id);
        return R.ok();
    }

    @ApiOperation("根据Id获取轮播")
    @GetMapping("getBannerBy/{id}")
    public R getBannerById(@PathVariable("id") String id) {
        CrmBanner banner = bannerService.getById(id);
        return R.ok().data("bannerInfo", banner);
    }

    @ApiOperation("修改轮播图")
    @PostMapping("/updateBanner")
    public R updateBanner(@RequestBody  BannerQuery banner) {
        bannerService.updateBannerById(banner);
        return R.ok();
    }



}

